package com.atguigu.interview.algo.test;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Jungle
 * @create 2023-09-10 22:24
 */
public class LruCacheDemo01<K, V> extends LinkedHashMap<K, V> {
    private int capacity;

    /**
     * initialCapacity – the initial capacity
     * loadFactor – the load factor
     * accessOrder – the ordering mode - true for access-order, false for insertion-order
     */
    public LruCacheDemo01(int capacity) {
        super(capacity, 0.75f, false);
        this.capacity = capacity;
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<K, V> eldest) {
        return super.size() > capacity;
    }

    public static void main(String[] args) {
        LruCacheDemo01 lru = new LruCacheDemo01(3);
        lru.put(1, "a");
        lru.put(2, "b");
        lru.put(3, "c");

        System.out.println(lru.keySet());
        lru.put(4, "d");
        System.out.println(lru.keySet());
        lru.put(3, "c");
        System.out.println(lru.keySet());
        lru.put(3, "c");
        System.out.println(lru.keySet());
        lru.put(5, "x");
        System.out.println(lru.keySet());
    }
}
